define(function () {

    var self, item;

    var gwat310_01 = app.Module.extend({

        init: function () {
            self = this;
            self._super({
                _tag: 'gwat310_01' // key, 对象放进pageModule
            }, win);

            item = app.cloneJSON(app.pageParameters['gwat310_01_page']);
            item.list.forEach(function (v) {
                v.GWCS050 = v.GWCS050 || '';//备注
                v.GWCS019 = v.GWCS019 || '';//序列号
                v.GWCS016 = (v.GWCS016 != undefined ? v.GWCS016.replace(" 00:00:00", "") : "") || '';//投入使用日期
                v.GWCS104 = v.GWCS104 || '';//合同编号
            });

            mui.ready(function () {
                new Vue({
                    el: doc.getElementById('gwat310_01'),
                    data: function () {
                        return {
                            item: item
                        }
                    },
                    methods: {
                        editGoods: function (good, index) {
                            app.openPage({
                                src: "gwat310/gwat310_02.html",
                                argsKey: 'gwat310_02_page',
                                args: {
                                    good: good,
                                    index: index
                                }
                            });
                        },
                        save: function () {
                            // wenchuan.li 20191203 先屏蔽掉
                            // if (!self.validate()) {
                            //     return;
                            // }
                            app.pageModule('p_gwat310').setValue(item);
                            app.back(1);
                        },
                        ok: function () {
                            // wenchuan.li 20191203 先屏蔽掉
                            // if (!self.validate()) {
                            //     return;
                            // }

                            var _subOrders = item.list;
                            var len = _subOrders.length;

                            var toData = function () {
                                var __subOrders = app.cloneJSON(_subOrders);
                                __subOrders.forEach(function (v) {
                                    delete v.GWAM017;
                                    delete v.GWAM016;
                                    delete v.CHARACTERISTICS;
                                });
                                item.list.forEach(function (order, index) {
                                    if (order.GWCSDATE != undefined) {
                                        order.GWCSDATE = order.GWCSDATE.replace(" 00:00:00", "");
                                        order.GWCSDOCDATE = order.GWCSDOCDATE.replace(" 00:00:00", "");
                                    }
                                })
                                app.ajax({
                                    xurl: "api/wx/gwat310/confirm",
                                    loading: true,
                                    xdata: {
                                        obj: app.jsonString(item)
                                    },
                                    cb: function (result) {
                                        if (result.ok) {
                                            app.toast('确认成功');
                                            app.pageModule('p_gwat310')._refresh();
                                            app.back(1);
                                        } else {
                                            app.toast(result.data.message);
                                        }
                                    }
                                });
                            };

                            var toSubmit = function (i) {
                                var item = _subOrders[i];
                                if (item.imgs && item.imgs.length > 0) {
                                    app.uploadLocalIds({
                                        imgs: item.imgs,
                                        cb: function (imgs) {
                                            delete item.imgs;
                                            if (imgs.length > 0) {
                                                var imgurl = "";
                                                for (var i = 0; i < imgs.length; i++) {
                                                    imgurl += (imgs[i].name + ",");
                                                }
                                                if (item.GWCS017) {
                                                    item.GWCS017 += ',' + imgurl.substring(0,imgurl.length-1);//图片地址
                                                } else {
                                                    item.GWCS017 = imgurl.substring(0,imgurl.length-1);//图片地址
                                                }
                                            }

                                            if (i == len - 1) {
                                                toData();
                                            } else {
                                                toSubmit(i + 1);
                                            }
                                        }
                                    });
                                } else {
                                    if (i == len - 1) {
                                        toData();
                                    } else {
                                        toSubmit(i + 1);
                                    }
                                }
                            };

                            app.confirm({
                                msg: '是否确认？',
                                ok: function (e) {
                                    app.showLoading();
                                    toSubmit(0);
                                }
                            });
                        }
                    }
                });
            });
            mui('.mui-scroll-wrapper').scroll({
                deceleration: 0.0005 //flick 减速系数，系数越大，滚动速度越慢，滚动距离越小，默认值0.0006
            });
        },
        validate: function () {
            for (var i = 0; i < item.list.length; i++) {
                // if (item.list[i].GWCS104 == '' || item.list[i].GWCS104 == null) {
                //     app.toast('NO.' + (i + 1) + '合同编号为空，请检查');
                //     return false;
                // } else if (item.list[i].GWCS019 == '' || item.list[i].GWCS019 == null) {
                //     app.toast('NO.' + (i + 1) + '序列号为空，请检查');
                //     return false;
                // } else
                    if (item.list[i].GWCS016 == '' || item.list[i].GWCS016 == null) {
                    app.toast('NO.' + (i + 1) + '投入使用日期为空，请检查');
                    return false;
                }
            }
            return true;
        },
        setValue: function (good, index) {
            Vue.set(item.list, index, good);
        }
    });
    return new gwat310_01();
});